package org.rcloud.io.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.model.Public;
import org.rcloud.io.service.PublicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/3/12.
 */
@RestController
@RequestMapping(value = "/public")
public class PublicController {
    @Autowired
    private PublicService publicService;

    /*
  * 获取所有的公共服务信息
  * */
    @ApiOperation(value="获取所有的公共服务信息", notes="获取所有的公共服务信息")
    @ResponseBody
    @RequestMapping(value = "/findPublic", produces = {"application/json;charset=UTF-8"})
    public PageVo<Public> findPublic(){
        return publicService.findPublic();
    }

    /*
  * 根据eid获取公共服务信息
  * */
    @ApiOperation(value = "根据eid获取公共服务信息", notes = "根据eid获取公共服务信息")
    @ResponseBody
    @RequestMapping(value = "/findByEid", produces = {"application/json;charset=UTF-8"})
    public ResultVo findByEid(@RequestBody String params) {
        JSONObject object = JSON.parseObject(params);
        return publicService.findByEid(object.getString("eid"));
    }

    /*
   * 保存公共服务信息
   * */
    @ApiOperation(value = "保存公共服务信息", notes = "保存公共服务信息")
    @ResponseBody
    @RequestMapping(value = "/savePublic", produces = {"application/json;charset=UTF-8"})
    public ResultVo savePublic(@RequestBody String params) {

        JSONObject object = JSON.parseObject(params);

        Map map = JSON.parseObject(object.toString());
        String eid = map.get("eid").toString();
        String name = map.get("name").toString();
        String address = map.get("address").toString();
        String longitude = map.get("longitude").toString();
        String latitude = map.get("latitude").toString();
        String etype = map.get("etype").toString();
        String summary = map.get("summary").toString();
        String report = map.get("report").toString();
        String username = map.get("username").toString();
        String userphone = map.get("userphone").toString();

        Public publ = new Public();
        publ.setEid(eid);
        publ.setName(name);
        publ.setAddress(address);
        publ.setLongitude(longitude);
        publ.setLatitude(latitude);
        publ.setEtype(etype);
        publ.setSummary(summary);
        publ.setReport(report);
        publ.setUsername(username);
        publ.setUserphone(userphone);
        publ.setFlag(1);

        return publicService.savePublic(publ);
    }

    /*
 * 根据id获取公共服务信息
 * */
    @ApiOperation(value = "根据id获取公共服务信息", notes = "根据id获取公共服务信息")
    @ResponseBody
    @RequestMapping(value = "/findById", produces = {"application/json;charset=UTF-8"})
    public ResultVo findById(@RequestBody String params) {
        String[] ids = params.split("=");
        int id = Integer.parseInt(ids[1]);
        return publicService.findById(id);
    }

    /*
    * 修改公共服务信息
    * */
    @ApiOperation(value="修改公共服务信息", notes="修改公共服务信息")
    @ResponseBody
    @RequestMapping(value = "/updatePublic", produces = {"application/json;charset=UTF-8"})
    public ResultVo updatePublic(@RequestBody String params) {
        JSONObject object = JSON.parseObject(params);
        Map map = JSON.parseObject(object.toString());

        int id = Integer.parseInt(map.get("id").toString());
        String eid = map.get("eid").toString();
        String name = map.get("name").toString();
        String address = map.get("address").toString();
        String longitude = map.get("longitude").toString();
        String latitude = map.get("latitude").toString();
        String etype = map.get("etype").toString();
        String summary = map.get("summary").toString();
        String report = map.get("report").toString();
        String username = map.get("username").toString();
        String userphone = map.get("userphone").toString();

        Public publ = new Public();
        publ.setId(id);
        publ.setEid(eid);
        publ.setName(name);
        publ.setAddress(address);
        publ.setLongitude(longitude);
        publ.setLatitude(latitude);
        publ.setEtype(etype);
        publ.setSummary(summary);
        publ.setUsername(username);
        publ.setUserphone(userphone);

        return publicService.updatePublic(publ);
    }
}
